package com.myth.safety.synchronized1;

/**
 * 例子:创建三个窗口卖票，总票数为100张.使用实现Runnable接口的方式
 * 存在线程的安全问题，待解决。
 *
 * @author
 * @create 2021/8/8
 */
class Window2 extends Thread{

    private static int ticket = 100;
    Object obj = new Object();
    @Override
    public void run() {
        synchronized (Window2.class) {
//        synchronized (obj) {
            while (true) {
                try {
                    sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (ticket > 0) {
                    System.out.println(getName() + ":卖票，票号为:" + ticket);
                    ticket--;
                } else {
                    break;
                }
            }
        }
    }
}

public class WindowTest2 {
    public static void main(String[] args) {
        Thread t1 = new Window2();
        Thread t2 = new Window2();
        Thread t3 = new Window2();

        t1.setName("窗口1");
        t2.setName("窗口2");
        t3.setName("窗口3");

        t1.start();
        t2.start();
        t3.start();

    }


}